package br.com.webhomesites.locacao.presenter;

import br.com.webhomesites.locacao.contracts.IManutencaoView;
import br.com.webhomesites.locacao.model.ManutencaoBO;
import br.com.webhomesites.padrao.model.ProdutoDAO;
import br.com.webhomesites.padrao.utils.ManipuladorDatas;
import br.com.webhomesites.padrao.utils.Mascarador;
import br.com.webhomesites.principal.EmissaoRelatorios;
import java.awt.Component;
import java.awt.event.*;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class ManutencaoPresenterActionListeners {

    static class AlterarActionListener implements ActionListener {

        private ManutencaoPresenter presenter;

        public AlterarActionListener(ManutencaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            presenter.setOperacao(presenter.UPDATE_STRATEGY);
            presenter.habilitarEdicao(true);

            IManutencaoView view = (IManutencaoView) presenter.getManutencaoView();

            view.enableBtnInserirItem(true);
            view.enableBtnRemoverItem(true);
            view.enableBtnCancelarManutencao(true);
            view.enableBtnSalvarManutencao(true);

        }
    }

    static class CancelarManutencaoActionListener implements ActionListener {

        private ManutencaoPresenter presenter;

        public CancelarManutencaoActionListener(ManutencaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {

            IManutencaoView view = (IManutencaoView) presenter.getManutencaoView();

            view.clearFieldsPainelManutencao();
            //view.setComboEmpresaManutencaosFocus();

            view.enableBtnNovaManutencao(true);
            view.enableBtnCancelarManutencao(false);
            view.enableBtnSalvarManutencao(false);


        }
    }

    static class CancelarItemManutencaoActionListener implements ActionListener {

        private ManutencaoPresenter presenter;

        public CancelarItemManutencaoActionListener(ManutencaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {

            IManutencaoView view = (IManutencaoView) presenter.getManutencaoView();

            view.clearFields();

            view.clearFieldsPainelIncluirProdutos();
            view.setComboProdutosFocus();

            view.enableBtnInserirItem(true);
            view.enableBtnRemoverItem(false);
            view.enableBtnCancelarItem(false);
            view.enableBtnSalvarItem(false);

        }
    }

    static class SalvarManutencaoActionListener implements ActionListener {

        private ManutencaoPresenter presenter;
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

        public SalvarManutencaoActionListener(ManutencaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            IManutencaoView view = (IManutencaoView) presenter.getManutencaoView();
            ManutencaoBO locacaoBO = presenter.getManutencaoBO();

            
            try {
                if (!ManipuladorDatas.trataDatasMaiorMenor((Date) formatter.parse(view.getDataSaidaManutencao()), (Date) formatter.parse(view.getDataPrevDevolucao()))) {
                    JOptionPane.showMessageDialog(null, "Data da saída maior que a data prevista para devolução!");
                } else {
                    presenter.getOperacao().execute();
                    
                    view.enableBtnNovaManutencao(true);
                    view.enableBtnCancelarManutencao(false);
                    view.enableBtnSalvarManutencao(false);
                    view.enableBtnSalvarItem(false);
                    view.enableBtnCancelarItem(false);
                    view.enableBtnRemoverItem(false);
                    view.enableBtnInserirItem(true);
                }
            } catch (ParseException ex) {
                Logger.getLogger(ManutencaoPresenterActionListeners.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    static class SalvarItemManutencaoActionListener implements ActionListener {

        private ManutencaoPresenter presenter;

        public SalvarItemManutencaoActionListener(ManutencaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            presenter.getOperacao().execute();
            presenter.setOperacao(presenter.INSERT_ITEM_STRATEGY);
            //presenter.habilitarEdicao(false);  

            //setUpViewListeners();

            IManutencaoView view = (IManutencaoView) presenter.getManutencaoView();

            view.enableBtnInserirItem(true);
            view.enableBtnSalvarItem(false);
            view.enableBtnCancelarItem(false);
            view.enableBtnRemoverItem(false);

        }
    }

    static class InserirManutencaoActionListener implements ActionListener {

        private ManutencaoPresenter presenter;

        public InserirManutencaoActionListener(ManutencaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            presenter.setOperacao(presenter.INSERT_STRATEGY);
            presenter.novoManutencao();

            IManutencaoView view = (IManutencaoView) presenter.getManutencaoView();

            presenter.habilitarEdicao(true);
            //view.clearFieldsPainelManutencao();            
            view.clearFieldsPainelIncluirProdutos();

            Calendar cal = Calendar.getInstance();
            Date date = cal.getTime();
            DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

            view.setDataSaidaManutencao(formatter.format(date));

            view.enableBtnNovaManutencao(false);
            view.enableBtnSalvarManutencao(true);
            view.enableBtnCancelarManutencao(true);

            view.enableBtnInserirItem(false);
            view.enableBtnSalvarItem(false);
            view.enableBtnRemoverItem(false);
            view.enableBtnCancelarItem(false);

            view.clearTableItensManutencao();
        }
    }

    static class InserirItemManutencaoActionListener implements ActionListener {

        private ManutencaoPresenter presenter;

        public InserirItemManutencaoActionListener(ManutencaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            presenter.setOperacao(presenter.INSERT_ITEM_STRATEGY);

            IManutencaoView view = (IManutencaoView) presenter.getManutencaoView();
            //view.removeAllitensComboDescricaoProduto();
            presenter.novoItemManutencao();

            presenter.habilitarEdicao(true);

            view.enableBtnInserirItem(false);
            view.enableBtnSalvarItem(true);
            view.enableBtnCancelarItem(true);
            view.enableBtnRemoverItem(false);

        }
    }

    static class RemoverItemManutencaoActionListener implements ActionListener {

        private ManutencaoPresenter presenter;

        public RemoverItemManutencaoActionListener(ManutencaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {

            if (JOptionPane.showConfirmDialog((Component) presenter.getManutencaoView(), "Deseja remover?", "Pergunta", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {

                IManutencaoView view = (IManutencaoView) presenter.getManutencaoView();

                presenter.setOperacao(presenter.REMOVE_ITEM_STRATEGY);
                presenter.getOperacao().execute();

                view.enableBtnInserirItem(true);
                view.enableBtnSalvarItem(false);
                view.enableBtnCancelarItem(false);
                view.enableBtnRemoverItem(false);

            }
        }
    }

    static class SairActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent evt) {
            System.exit(0);
        }
    }

    static class CampoKeyListener implements KeyListener {

        private ManutencaoPresenter presenter;
        IManutencaoView view;
        Mascarador mask;

        public CampoKeyListener(ManutencaoPresenter presenter) {
            this.presenter = presenter;
            this.view = (IManutencaoView) presenter.getManutencaoView();
            mask = new Mascarador();
        }

        @Override
        public void keyTyped(KeyEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void keyPressed(KeyEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void keyReleased(KeyEvent e) {
//            view.setDataManutencao(mask.getMascara(view.getDataManutencao(), mask.DATA, e));
            view.setDataPrevDevolucao(mask.getMascara(view.getDataPrevDevolucao(), mask.DATA, e));
//            view.setValorItemManutencao(mask.getMascara(view.getValorItemManutencao(), mask.MOEDA, e));
//            view.setValorProdutoManutencao(mask.getMascara(view.getValorItemManutencao(), mask.MOEDA, e));
//            view.setValorTotalManutencao(mask.getMascara(view.getValorItemManutencao(), mask.MOEDA, e));
//            DecimalFormat df = new DecimalFormat("##,###.00");
//            //view.setValorProdutoManutencao(df.format(view.getValorProdutoManutencao()));
//            Currency cur = new Currency();
//            System.out.println(cur.getMoeda(Double.toString(view.getValorProdutoManutencao())));
        }
    }

    static class ComboStateListener implements ItemListener {

        private ManutencaoPresenter presenter;
        IManutencaoView view;

        public ComboStateListener(ManutencaoPresenter presenter) {
            this.presenter = presenter;
            this.view = (IManutencaoView) presenter.getManutencaoView();

        }

        @Override
        public void itemStateChanged(ItemEvent ie) {

            System.out.println("Mudou de estado");
            ProdutoDAO produtoDAO = new ProdutoDAO();
            if (!view.getDescricaoItemManutencao().equals("1")) {
                presenter.updateViewItemFromProdutoModel(produtoDAO.findByDescricaoUnica(view.getDescricaoItemManutencao()));
            }
        }
    }

    static class EnterListener implements ActionListener {

        private ManutencaoPresenter locacaoPresenter;

        public EnterListener(ManutencaoPresenter locacaoPresenter) {
            this.locacaoPresenter = locacaoPresenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            ManutencaoBO locacaoBO = new ManutencaoBO();
            //locacaoBO.setCalcDataPrevDevolucao(locacaoPresenter);         

        }
    }

    static class TableItensManutencaoMouseListener extends MouseAdapter {

        private ManutencaoPresenter manutencaoPresenter;

        public TableItensManutencaoMouseListener(ManutencaoPresenter manutencaoPresenter) {
            this.manutencaoPresenter = manutencaoPresenter;
        }

        @Override
        public void mouseClicked(MouseEvent e) {

            manutencaoPresenter.updateModelFromJTable();

        }
    }

    static class EmitirContratoActionListener implements ActionListener {

        private ManutencaoPresenter presenter;

        public EmitirContratoActionListener(ManutencaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            try {
                Long n = presenter.getManutencaoModel().getId();
                Integer n1 = Integer.valueOf(n.toString());
                EmissaoRelatorios teste = new EmissaoRelatorios();

                teste.imprimirContrato(n1,4);

                IManutencaoView view = presenter.getManutencaoView();

                //view.enableBtnAprovarManutencao(true);

            } catch (Exception e) {
                System.out.println("Capacidade do Integer estourou.");
            }
        }
    }

    static class AprovarManutencaoActionListener implements ActionListener {

        private ManutencaoPresenter presenter;

        public AprovarManutencaoActionListener(ManutencaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {

            IManutencaoView view = presenter.getManutencaoView();

            view.changeStatusManutencao();
            //view.enableBtnPagamentoManutencao(true);

        }
    }

    static class LostFocusListener implements FocusListener {

        private ManutencaoPresenter locacaoPresenter;

        public LostFocusListener(ManutencaoPresenter locacaoPresenter) {
            this.locacaoPresenter = locacaoPresenter;
        }

        @Override
        public void focusGained(FocusEvent e) {
            ManutencaoBO locacaoBO = new ManutencaoBO();
            //locacaoBO.setCalcDataPrevDevolucao(locacaoPresenter);

        }

        @Override
        public void focusLost(FocusEvent e) {
//            DecimalFormat df = new DecimalFormat("##,###.00");
//            locacaoPresenter.getManutencaoView().setValorProdutoManutencao(df.format(locacaoPresenter.getManutencaoView().getValorProdutoManutencao()));
            ManutencaoBO locacaoBO = new ManutencaoBO();
            //locacaoBO.setCalcDataPrevDevolucao(locacaoPresenter);

        }
    }

    static class MoedaFocusListener implements FocusListener {

        private ManutencaoPresenter locacaoPresenter;

        public MoedaFocusListener(ManutencaoPresenter locacaoPresenter) {
            this.locacaoPresenter = locacaoPresenter;
        }

        @Override
        public void focusGained(FocusEvent e) {
        }

        @Override
        public void focusLost(FocusEvent e) {
            DecimalFormat df = new DecimalFormat("##,###.00");
//            if (!locacaoPresenter.getManutencaoView().getValorProdutoManutencao().toString().equals("0.0")) {
//                locacaoPresenter.getManutencaoView().setValorProdutoManutencao(df.format((locacaoPresenter.getManutencaoView().getValorProdutoManutencao())));
//            }
//            if (!locacaoPresenter.getManutencaoView().getValorItemManutencao().toString().equals("0.0")) {
//                locacaoPresenter.getManutencaoView().setValorItemManutencao((df.format((locacaoPresenter.getManutencaoView().getValorItemManutencao()))));
//            }
//            if (!locacaoPresenter.getManutencaoView().getValorTotalManutencao().toString().equals("0.0")) {
//                locacaoPresenter.getManutencaoView().setValorTotalManutencao(df.format((locacaoPresenter.getManutencaoView().getValorTotalManutencao())));
//            }

        }
    }
    
    static class TableManutencaoMouseListener extends MouseAdapter {  
        private ManutencaoPresenter manutencaoPresenter;  
          
        public TableManutencaoMouseListener(ManutencaoPresenter manutencaoPresenter) {  
            this.manutencaoPresenter = manutencaoPresenter;  
        }  
          
        @Override
        public void mouseClicked(MouseEvent e) {  
            System.out.println("Ação sendo ouvida pela tabela de locações!");
            manutencaoPresenter.updateModelManutencaoFromJTable();
   
        }  
    }
    
    static class BuscaAvancadaActionlistener implements ActionListener {
        
        ManutencaoPresenter presenter;

        public BuscaAvancadaActionlistener(ManutencaoPresenter presenter) {
           this.presenter = presenter;
        }
        
        @Override
        public void actionPerformed(ActionEvent e) {
            presenter.buscaAvancada();
        }
        
    }
}
